<?php 
	require_once($_SERVER['DOCUMENT_ROOT']."/model/data_access.php");
    require_once($_SERVER['DOCUMENT_ROOT']."/model/helper.php");
	require_once($_SERVER['DOCUMENT_ROOT']."/configures/global.php");
    $source = array_key_exists("src",$_GET)?$_GET["src"]:"";
    $lan = get_lan();
    $cate = array_key_exists("cate",$_GET)?$_GET["cate"]:Categories::ALL;
    $cate = new Categories($cate);
    $divided_src = explode(",",$source,4);
    $divided_src = array_map(function($item){return empty($item)?"[\w ]*":$item;},$divided_src);
    if (count($divided_src) == 1)
    {
        $result_country = search_countries(
            $divided_src[0],
            NULL,
            $cate,
            function($data)use($lan) {
                return $data["ContinentName_{$lan}"] . ",". $data["CountryName_{$lan}"];
            });
        $result_city = search_cities(
            $divided_src[0],
            NULL,
            NULL,
            $cate,
            function($data)use($lan)
            {
                return $data["ContinentName_{$lan}"] .",". $data["CountryName_{$lan}"] .",". $data["CityName_{$lan}"];
            }
        );
        $result = array_merge($result_city , $result_country);
    }
    else if (count($divided_src) == 2)
	{
        $result_country = search_countries(
            $divided_src[1],
            $divided_src[0],
            $cate,
            function($data)use($lan)
            {
                return $data["ContinentName_{$lan}"] .",". $data["CountryName_{$lan}"];
            }
            );
        $result_city = search_cities(
            $divided_src[1],
            NULL,
            $divided_src[0],
            $cate,
            NULL,
            function($data)use($lan)
            {
                return $data["ContinentName_{$lan}"] .",". $data["CountryName_{$lan}"] .",". $data["CityName_{$lan}"];
            }
        );
      $result = array_merge($result_city , $result_country);
   }
   else if (count($divided_src) == 3)
   {
        $result_city = search_cities(
            $divided_src[2],
            $divided_src[1],
            $divided_src[0],
            $cate,
            function($data)use($lan)
            {
                return $data["ContinentName_{$lan}"] .",". $data["CountryName_{$lan}"] .",". $data["CityName_{$lan}"];
            }
            );
		$result = $result_city;
  }    
   $arr = [];

    foreach ( array_unique($result) as $key=>$value)
   {
        if (!empty($value))
        {
            array_push($arr,$value);
        }
   }
	echo json_encode($arr);
?>